package com.study.ui.home

import android.content.Intent
import android.view.View
import androidx.lifecycle.observe
import com.lbj.core.ext.runWork
import com.study.R
import com.study.app.app.BaseActivity
import com.study.databinding.ActivityMainBinding
import com.study.ui.home.adapter.MainListAdapter
import com.study.ui.test.TestActivity

class MainActivity : BaseActivity<MainVM, ActivityMainBinding>() {
    override fun layoutId() = R.layout.activity_main

    private val mAdapter by lazy { MainListAdapter() }
    private var index = 0

    override fun initView() {
        VM.productListMLD.observe(this) { work ->
            runWork(work, {
                it.list.map { item ->
                    item.id = (index++).toString()
                    VM.list.add(item)
                }
                mAdapter.submitList(ArrayList(VM.list))
            }, {})
        }
    }

    override fun bind() {
        binding.vm = VM
        binding.persenter = Presenter()
        binding.adapter = mAdapter
    }

    inner class Presenter {
        fun onRequestClick1(v: View) {
            showLoading()
            VM.getProductList()
        }

        fun onUploadData(v: View) {
            startActivity(Intent(this@MainActivity, TestActivity::class.java))
        }
    }
}
